Skip to content

sdk%fix: fix incorrect serialisation constants (max script size, P2P msg len), fix MnListDiffPayload serialisation routines#3

Merged
kwvg merged 9 commits intodashpay:developfrom
kwvg:fixups
May 5, 2026
Merged

sdk%fix: fix incorrect serialisation constants (max script size, P2P msg len), fix MnListDiffPayload serialisation routines#3
kwvg merged 9 commits intodashpay:developfrom
kwvg:fixups

Conversation

@kwvg
Copy link
Copy Markdown
Collaborator

@kwvg kwvg commented May 5, 2026

Additional Information

  • Depends on sdk%refac: switch from hex to hex-conservative #2

  • The Script decoder applied the interpreter limit of MAX_SCRIPT_SIZE instead of the serialization limit of MAX_SIZE which meant otherwise legal (though unspendable) scripts caused errors when being parsed. This has been fixed.

  • MAX_P2P_PAYLOAD has been updated to match the limits set by Dash Core with MAX_PROTOCOL_MESSAGE_LENGTH, there's little benefit to being more permissive than the reference implementation.

    • Likewise, MAX_GOV_DATA has been dropped and restricted against MAX_P2P_PAYLOAD, the practical limit that it'll press against as governance messages propagate over the P2P system.
  • MnListDiffPayload errantly assumed that the commitments in a MnListDiff are serialized within an extra payload envelope. This has been resolved alongside a version decode bug that was discovered when a KAT vector was committed and tested against.

Breaking Changes

  • dash-type's PlatformNodeId, BlsPublicKeyBytes, BlsSignatureBytes, EcdsaPublicKeyBytes and EcdsaSignatureBytes and dash-script's KeyId's {as,to}_byte_array() are now {as,to}_bytes().

  • dash-p2p-core's FilterType and ProtocolVersion's to_inner() is now value().

How Has This Been Tested?

cargo test --features full,_internal
cargo clippy --features full,_internal --tests
cargo fmt --check

Checklist

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

@kwvg kwvg added this to the 0.1 milestone May 5, 2026
@kwvg kwvg self-assigned this May 5, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

📝 Walkthrough

Walkthrough

This PR reorganizes byte accessor APIs across the codebase, updates P2P payload limits and masternode list versioning logic, increases the script decoder size limit, renames a uint accessor method, and configures feature-gating for upstream dependencies. The changes span multiple Rust packages and their tests.

Changes

Byte Accessor API Rename

Layer / File(s) Summary
Type Definition Updates
pkgs/types/src/hex.rs, pkgs/script/src/key_id.rs
make_bytes! and KeyId replace to_byte_array() and as_byte_array() methods with to_bytes() and as_bytes().
Usage Site Updates
pkgs/script/src/lib.rs, pkgs/primitives/src/payload/proregtx.rs
Callers updated to invoke new method names (to_bytes(), as_bytes()) when converting byte types.
Documentation
docs/guide_rust.md
Naming guidance table updated with new method names in conversion examples.

P2P Payload Limits and Masternode List Versioning

Layer / File(s) Summary
Payload Limit Constant
pkgs/p2p_core/src/encode.rs
MAX_P2P_PAYLOAD reduced from 4,000,000 to 3,145,728 (3 MiB).
Governance Decoding
pkgs/p2p_core/src/primitives/governance.rs
Governance object data field bound changed from removed MAX_GOV_DATA to the new MAX_P2P_PAYLOAD limit.
Masternode List Versioning
pkgs/p2p_core/src/primitives/mn_list.rs
SimplifiedMnListEntry decoding and encoding now driven by per-entry version field instead of enclosing diff-level version; MnListDiffPayload loop updated accordingly; Commitment decoding switched to decode_inner().
Test Infrastructure
pkgs/p2p_core/Cargo.toml, pkgs/p2p_core/tests/mnlistdiff.rs
Dev-dependencies added (serde, serde_json, json5); new KAT test module with corpus-driven decode and round-trip assertions added.

Script Decoder Size Limit Update

Layer / File(s) Summary
Decoder Configuration
pkgs/primitives/src/script.rs
ScriptDecoder size limit increased from 10 KiB (MAX_SCRIPT_SIZE) to 32 MiB (MAX_SIZE).

Uint Accessor Method Rename

Layer / File(s) Summary
Macro-Generated Method
pkgs/types/src/uint.rs
make_uint! macro replaces to_inner(self) accessor with value(self) for wrapped integer access.

Num Feature Configuration

Layer / File(s) Summary
Feature Dependency
pkgs/num/Cargo.toml
std feature now enables upstream bitcoin-consensus-encoding/std capability instead of remaining empty.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the three main changes: fixing incorrect serialization constants (MAX_SCRIPT_SIZE and P2P message length) and fixing MnListDiffPayload serialization routines.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description clearly explains the purpose of the changes, including fixes to Script decoder limits, P2P payload constants, MnListDiffPayload serialization, and API naming updates.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

Note

This pull request has no conflicts! 🎊 🎉 🎊

@kwvg kwvg marked this pull request as ready for review May 5, 2026 12:24
@kwvg
Copy link
Copy Markdown
Collaborator Author

kwvg commented May 5, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkgs/p2p_core/src/primitives/mn_list.rs (1)

113-119: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard Evo/version invariants before serializing SimplifiedMnListEntry.

At Line 113, serialization can silently produce malformed bytes when mn_type == MnType::Evo but platform_http_port or platform_node_id is None. Also, version < 2 with non-regular mn_type can produce non-roundtrippable output. Please enforce these invariants before writing bytes.

💡 Minimal fail-fast guard (example)
 pub(crate) fn encode(&self, buf: &mut Vec<u8>) {
+  assert!(
+    self.version >= 2 || self.mn_type == MnType::Regular,
+    "mn_type requires version >= 2"
+  );
+
   buf.extend_from_slice(&self.version.to_le_bytes());
   buf.extend_from_slice(&self.pro_reg_tx_hash.to_bytes());
   buf.extend_from_slice(&self.confirmed_hash.to_bytes());
   buf.extend_from_slice(&self.service.addr);
   buf.extend_from_slice(&self.service.port.to_be_bytes());
   buf.extend_from_slice(&self.operator_key.0);
   buf.extend_from_slice(&self.voting_key_id.0);
   buf.push(u8::from(self.is_valid));
   if self.version >= 2 {
     buf.extend_from_slice(&self.mn_type.to_u16().to_le_bytes());
   }
   if self.mn_type == MnType::Evo {
-    if let Some(port) = self.platform_http_port {
-      buf.extend_from_slice(&port.to_le_bytes());
-    }
-    if let Some(ref nid) = self.platform_node_id {
-      buf.extend_from_slice(&nid.0);
-    }
+    let port = self.platform_http_port.expect("Evo entry missing platform_http_port");
+    let nid = self.platform_node_id.as_ref().expect("Evo entry missing platform_node_id");
+    buf.extend_from_slice(&port.to_le_bytes());
+    buf.extend_from_slice(&nid.0);
   }
 }

As per coding guidelines, "Use newtypes over primitives when semantics differ, enums over booleans, and make invalid states unrepresentable".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkgs/p2p_core/src/primitives/mn_list.rs` around lines 113 - 119, Before
emitting bytes in the SimplifiedMnListEntry serialization path, validate the
invariants: if self.mn_type == MnType::Evo then ensure
self.platform_http_port.is_some() and self.platform_node_id.is_some() and return
an error (or panic) if missing; likewise if self.version < 2 then ensure
self.mn_type == MnType::Regular (or return an error) so non-regular types aren't
serialized for older versions. Add these checks at the start of the
serialization routine (the impl that writes buf for SimplifiedMnListEntry) and
fail fast with a clear error message mentioning the field(s)
(platform_http_port, platform_node_id, mn_type, version) so malformed/
non-roundtrippable bytes are never produced.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@pkgs/p2p_core/src/primitives/mn_list.rs`:
- Around line 113-119: Before emitting bytes in the SimplifiedMnListEntry
serialization path, validate the invariants: if self.mn_type == MnType::Evo then
ensure self.platform_http_port.is_some() and self.platform_node_id.is_some() and
return an error (or panic) if missing; likewise if self.version < 2 then ensure
self.mn_type == MnType::Regular (or return an error) so non-regular types aren't
serialized for older versions. Add these checks at the start of the
serialization routine (the impl that writes buf for SimplifiedMnListEntry) and
fail fast with a clear error message mentioning the field(s)
(platform_http_port, platform_node_id, mn_type, version) so malformed/
non-roundtrippable bytes are never produced.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 06e6fb7d-47b2-4a3e-b53e-14a2e2936bf5

📥 Commits

Reviewing files that changed from the base of the PR and between 79d9be0 and b042964.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (15)
  • contrib/lint/lint_javascript.py
  • docs/guide_rust.md
  • pkgs/num/Cargo.toml
  • pkgs/p2p_core/Cargo.toml
  • pkgs/p2p_core/corpus/mnlistdiff.json5
  • pkgs/p2p_core/src/encode.rs
  • pkgs/p2p_core/src/primitives/governance.rs
  • pkgs/p2p_core/src/primitives/mn_list.rs
  • pkgs/p2p_core/tests/mnlistdiff.rs
  • pkgs/primitives/src/payload/proregtx.rs
  • pkgs/primitives/src/script.rs
  • pkgs/script/src/key_id.rs
  • pkgs/script/src/lib.rs
  • pkgs/types/src/hex.rs
  • pkgs/types/src/uint.rs

@kwvg kwvg merged commit 0238912 into dashpay:develop May 5, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant